package day29;

import day26.DBUtil;

import java.sql.*;


public class Text2 {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        try {
            con = DBUtil.getConnection();
            String sql = "insert into s_student(sname, sex, gradeid) values(?,?,?)";
            preparedStatement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, "wew");
            preparedStatement.setString(2, "男");
            preparedStatement.setInt(3, 1);
            int count = preparedStatement.executeUpdate();
            ResultSet rs = preparedStatement.getGeneratedKeys();
            if (rs.next()) {
                System.out.println("主键值为：" + rs.getObject(1));
            }
            System.out.println(count > 0 ? "添加成功！" : "添加失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
